package com.koron.plan.web.patrolplan.service.impl;

import cn.hutool.core.lang.Assert;
import com.koron.common.core.util.CodeTools;
import com.koron.common.core.util.StringUtils;
import com.koron.plan.web.common.CommonUtils;
import com.koron.plan.web.patrolplan.bean.entity.PatrolPlanEnvBean;
import com.koron.plan.web.patrolplan.mapper.PatrolPlanEnvMapper;
import com.koron.plan.web.patrolplan.service.PatrolPlanEnvService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author twcao
 * @title: PatrolPlanEnvServiceImpl
 * @projectName GDH_EAM_V2
 * @description: 巡视环境serviceImpl
 * @date 2021/10/2714:01
 */
@Service
public class PatrolPlanEnvServiceImpl implements PatrolPlanEnvService {

    /**
     * 通过ID查询单条数据
     *
     * @param factory
     * @param id      主键
     * @return 实例对象
     */
    @Override
    public PatrolPlanEnvBean queryById(SessionFactory factory, String id) {
        return null;
    }

    /**
     * 新增/编辑数据
     *
     * @param factory
     * @param bean    实例对象
     * @return 实例对象
     */
    @Override
    public PatrolPlanEnvBean saveOrUpdate(SessionFactory factory, PatrolPlanEnvBean bean) {
        Assert.notNull(bean, "环境不能为空");
        List<PatrolPlanEnvBean> beans = saveOrUpdate(factory, Arrays.asList(bean));
        return beans.get(0);
    }

    /**
     * 新增/编辑数据
     *
     * @param factory
     * @param beans   实例对象
     * @return 实例对象
     */
    @Override
    public List<PatrolPlanEnvBean> saveOrUpdate(SessionFactory factory, List<PatrolPlanEnvBean> beans) {
        if(CollectionUtils.isEmpty(beans)) {
            return beans;
        }
        PatrolPlanEnvMapper mapper = factory.getMapper(PatrolPlanEnvMapper.class);
        CommonUtils.sorted(beans);
        CommonUtils.update(beans);
        beans.stream().forEach(env -> env.setId(StringUtils.isBlank(env.getId()) ? CodeTools.getCode32() : env.getId()));
        mapper.saveOrUpdate(beans);
        return beans;
    }

    /**
     * 通过主键删除数据
     *
     * @param factory
     * @param id      主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(SessionFactory factory, String id) {
        PatrolPlanEnvMapper mapper = factory.getMapper(PatrolPlanEnvMapper.class);
        return mapper.deleteById(id) > 0;
    }

    /**
     * 通过巡视点id删除数据
     *
     * @param factory
     * @param pointId 主键
     * @return 是否成功
     */
    @Override
    public boolean hiddenByPointId(SessionFactory factory, String pointId) {
        PatrolPlanEnvMapper mapper = factory.getMapper(PatrolPlanEnvMapper.class);
        return mapper.hiddenByPointId(pointId) > 0;
    }

    @Override
    public boolean deleteByPointId(SessionFactory factory, String pointId) {
        PatrolPlanEnvMapper mapper = factory.getMapper(PatrolPlanEnvMapper.class);
        return mapper.deleteByPointId(pointId) > 0;
    }
}
